/*
 * RefBoxClient.h
 *
 *  Created on: 2010-10-02
 *      Author: kamil
 */

#ifndef REFBOXCLIENT_H_
#define REFBOXCLIENT_H_

//#include <log4cxx/logger.h>
#include <boost/asio.hpp>

/**
 * Referee box game state packet structure
 */
struct GameStatePacket {
	char cmd;				 	///< current referee command
	unsigned char cmd_counter; 	///< increments each time new command is set
	unsigned char goals_blue; 	///< current score for blue team
	unsigned char goals_yellow; ///< current score for yellow team
	unsigned short time_remaining; ///< seconds remaining for current game stage (network byte order)
};

enum RefereeCmd{
	CMD_HALT = 1001,
	CMD_STOP,
	CMD_READY,
	CMD_START,
	CMD_BEGIN_FIRST_HALF,
	CMD_BEGIN_HALF_TIME,
	CMD_BEGIN_SECOND_HALF,
	CMD_BEGIN_OVERTIME_HALF_1,
	CMD_BEGIN_OVERTIME_HALF_2,
	CMD_BEGIN_PENALTY_SHOOTOUT,
	CMD_KICKOFF_BLUE,
	CMD_KICKOFF_YELLOW,
	CMD_PENALTY_BLUE,
	CMD_PENALTY_YELLOW,
	CMD_DIRECT_FREE_KICK_BLUE,
	CMD_DIRECT_FREE_KICK_YELLOW,
	CMD_INDIRECT_FREE_KICK_BLUE,
	CMD_INDIRECT_FREE_KICK_YELLOW,
	CMD_TIMEOUT_BLUE,
	CMD_TIMEOUT_YELLOW,
	CMD_TIMEOUT_END_BLUE,
	CMD_TIMEOUT_END_YELLOW,
	CMD_GOAL_SCORED_BLUE,
	CMD_GOAL_SCORED_YELLOW,
	CMD_DECREASE_GOAL_SCORE_BLUE,
	CMD_DECREASE_GOAL_SCORE_YELLOW,
	CMD_YELLOW_CARD_BLUE,
	CMD_YELLOW_CARD_YELLOW,
	CMD_RED_CARD_BLUE,
	CMD_RED_CARD_YELLOW,
	CMD_CANCEL,
	CMD_UNDEFINED = 1111
};

/**
 * Client class for SLL Referee Box application
 * For details see: http://small-size.informatik.uni-bremen.de/referee:start
 */
class RefBoxClient {
public:
	/**
	 * Tries to read data from socket. If there is a new command, return true (it means that when the same
	 * command is read, it still returns false!).
	 */
	RefereeCmd getLastCommand() const { return lastCmd; }
public:
	RefBoxClient(boost::asio::io_service & io_service);
	~RefBoxClient();

private:
	void prepareRead();
	void handleRead(const boost::system::error_code & error, std::size_t);

	/**
	 * Maps character command from packet to RefereeCmd enum
	 */
	RefereeCmd parseCommand(char command);

private:
	boost::asio::io_service & io_service;
	boost::asio::ip::udp::socket socket;

//	static log4cxx::LoggerPtr logger;

	char data[5];
	GameStatePacket packet;
	RefereeCmd lastCmd;
	int lastCmdNr;
};


#endif /* REFBOXCLIENT_H_ */
